package com.my.gesturelockandwakedemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.my.gesturelockandwakedemo.MainActivity;
import com.my.gesturelockandwakedemo.R;
import com.my.gesturelockandwakedemo.util.CacheActivity;
import com.my.gesturelockandwakedemo.util.SharedPrefUtil;

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private Button btnLogin;
    private EditText etUserName, etPassword;// 返回计时
    private long mExitTime = 0;
    private SharedPrefUtil sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        sp = new SharedPrefUtil(this, "MyGestureLock");
        initViews();
        initOnClickEvents();
    }

    @Override
    public void initViews() {
        super.initViews();
        btnLogin = (Button) findViewById(R.id.btn_login);
        etUserName = (EditText) findViewById(R.id.et_user_name);
        etPassword = (EditText) findViewById(R.id.et_password);
    }

    @Override
    public void initOnClickEvents() {
        super.initOnClickEvents();
        btnLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login: // 登录按钮
                if (sp.getString("userId", "").isEmpty()) {
                    entryCheck();
                } else if (sp.getString("userId", "").equals("0")) {
                    entryCheck();
                } else {
                    Toast.makeText(this, "已登录", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    /**
     * 输入检查
     */
    private void entryCheck() {
        if (etUserName.getText().toString().isEmpty()) {
            Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
        } else if (etUserName.getText().toString().equals("q")) {
            sp.putString("userId", "888");
            sp.commit();
            goIntent(MainActivity.class);
            CacheActivity.finishSingleActivity(this);
        } else {
            Toast.makeText(this, "账户名或密码错误", Toast.LENGTH_SHORT).show();
        }
    }


    private void goIntent(Class cla) {
        Intent intent = new Intent(this, cla);
        startActivity(intent);
        CacheActivity.finishSingleActivity(this);
    }

}
